package com.neuedu.his.service;



import com.neuedu.his.common.Page;
import com.neuedu.his.common.utils.ParamUtils;
import com.neuedu.his.dao.DeptDao;
import com.neuedu.his.dao.RegistLevelDao;
import com.neuedu.his.dao.UmsUserDao;
import com.neuedu.his.entity.UmsUser;
import com.neuedu.his.common.core.BaseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;



public class UmsUserService implements BaseService<UmsUser>   {

    private UmsUserDao dao = new UmsUserDao();
    private DeptDao deptDao = new DeptDao();
    private RegistLevelDao registLevelDao = new RegistLevelDao();


    public Page queryList(HttpServletRequest request){
        int pageNo = 1;
        int size = 10;

        String paramPageNo = request.getParameter("pageNo");
        String paraSize = request.getParameter("size");

        if(!Objects.isNull(paramPageNo)){
            pageNo = Integer.parseInt(paramPageNo);
        }
        if(!Objects.isNull(paraSize)){
            size = Integer.parseInt(paraSize);
        }

        List paraList = new ArrayList();
        String where  = "where 1=1   ";

        Object[] params = paraList.toArray();
        long total  = dao.selectCount(where,params);
        List<UmsUser> data = dao.selectPage(where,pageNo,size,params);

        return  Page.getInstatnce(pageNo ,size , data , total);
    }


    public boolean save(HttpServletRequest req) {

        //TODO  通过工具类生成   req   -- > UmsUser
        UmsUser  umsUser   =  ParamUtils.getParam(UmsUser.class,req);
        return  dao.insert(umsUser) >0;
    }

    public boolean update(HttpServletRequest req) {
        //TODO  通过工具类生成   req   -- > UmsUser
        UmsUser  umsUser   =  ParamUtils.getParam(UmsUser.class,req);
        return  dao.updateById( umsUser ) >0;
    }


    public  UmsUser queryById(HttpServletRequest req) {
        String userid = req.getParameter("userid");
        String where = " where userid = ?";
        Object params[] = {userid};
        return dao.selectOne(where,params);
    }


    /**
     * 根据主键删除
     * @param req
     * @return
     */
    public boolean deleteById(HttpServletRequest req) {
        String userid = req.getParameter("userid");
        return dao.deleteById(Integer.parseInt(userid))>0;
    }

    /**
     * 初始化修改页面
     * @param request
     * @return
     */
    public Map initEdit(HttpServletRequest request) {

        Map  res = new HashMap();
        res.put("deptList",deptDao.selectList("where del = 1")); //
        res.put("levelList",registLevelDao.selectList("")); //没有 del createtime

        return  res;
    }
}
